package cn.tedu.service.trolley.Impl;


import cn.tedu.commons.response.JsonResult;
import cn.tedu.mapper.trolley.TrolleyMapper;
import cn.tedu.pojo.dto.trolley.TrlyAddDTO;
import cn.tedu.pojo.entity.Trolley;


import cn.tedu.pojo.vo.tryvo.TrlyStandardVO;
import cn.tedu.service.trolley.ITrolleyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;


@Service
@Slf4j
public class TrolleyServiceImpl implements ITrolleyService {
    @Autowired
    private TrolleyMapper trolleyMapper;
    @Override
    public void addTrly(TrlyAddDTO trlyAddDTO) {
        log.info("::::::::::::::{}",trlyAddDTO);
        Trolley trolleyNew = new Trolley();
        BeanUtils.copyProperties(trlyAddDTO,trolleyNew);
        trolleyNew.setGmtCreate(LocalDateTime.now());
        trolleyNew.setGmtModified(LocalDateTime.now());
        trolleyNew.setIsDelete(1);
        trolleyMapper.saveToly(trolleyNew);

    }

    @Override
    public JsonResult trolleyByUserId(Long id) {
        System.out.println(id);
        int i = trolleyMapper.deleteTolyById(id);
        if (i!=1){
            throw new RuntimeException("删除失败，订单不存在");

        }
        return JsonResult.ok("删除成功");
    }

    @Override
    public JsonResult selectTrly(Long id ) {
        System.out.println(id);
        TrlyStandardVO selectToly = trolleyMapper.selectctTolyById(id);
        System.out.println(selectToly);
        return JsonResult.ok(selectToly);



    }

    @Override
    public JsonResult updateTrly(Long id,Long commodityNumber) {
        int i = trolleyMapper.updateQuantityTolyById(id,commodityNumber);
        if (i!=1) {
            throw new RuntimeException("修改的订单不存在");
        }
        return JsonResult.ok("修改完成");
    }

//    @Override
//    public JsonResult updateTrly(TrlyUpdateDTO trlyUpdateDTO) {
//        Trolley trolley = new Trolley();
//        BeanUtils.copyProperties(trlyUpdateDTO,trolley);
//        trolley.setGmtCreate(LocalDateTime.now());
//        trolley.setGmtModified(LocalDateTime.now());
//        trolley.setIsDelete(1);
//        trolleyMapper.saveToly(trolley);
//        return JsonResult.ok()

    }











